
import os
import random
import tkinter
import tkinter.messagebox

import numpy as np
from PIL import Image, ImageTk
from tkinter.filedialog import askdirectory
from siamese import Siamese

file = ""
global shippathlist
selectedShip = []

def predict():
    image_1 = Image.open(selectedShip[0])
    image_2 = Image.open(selectedShip[1])
    probability = model.detect_image(image_1, image_2).item()
    probability = "%.2f"%(float(probability)*100)+"%"
    # probability = "100%"
    value.set(probability)
    # result_data_Text.insert(0,probability)

#选择预配对的
def clickPre():
    selectedShip.clear()
    key = random.randint(0,len(shippathlist)/2)
    changePic(shippathlist[key],shippathlist[key+1])
    selectedShip.append(shippathlist[key])
    selectedShip.append(shippathlist[key+1])
    print(shippathlist[key])
    print(shippathlist[key+1])

def randint_digit(low, high, cutoff):
    digit_list = list(range(low, high))
    if type(cutoff) is int:  # 只需要剔除一个值
        if cutoff in digit_list:  # 如果需要剔除的值不存在，则不执行剔除操作
            digit_list.remove(cutoff)
    else:
        for i in cutoff:  # 需要剔除多个值的情况
            if i not in digit_list:  # 如果需要剔除的值不存在，则不执行剔除操作
                continue
            digit_list.remove(i)
    np.random.shuffle(digit_list)
    return digit_list.pop()  # 生成的序列打乱并且返回当前的随机值

#随机选两张
def clickRandom():
    selectedShip.clear()
    r1 = random.randint(0, len(shippathlist))
    r2 = randint_digit(0, len(shippathlist),(r1,r1+1,r1-1))
    selectedShip.append(shippathlist[r1])
    selectedShip.append(shippathlist[r2])
    print(selectedShip)
    changePic(shippathlist[r1], shippathlist[r2])

#展示图片
def changePic(img1,img2):
    img1=Image.open(img1)
    img2=Image.open(img2)
    # w,h=img.size
    # if varPattern.get()==0:
    #     if w>width:
    #         h=int(h*width/w)
    #     if h>height:
    #         w=int(w*height/w)
    # if w>width:
    #         w=width
    # if h>height:
    #     h=height
    #
    # img=img.resize((w,h))
    im1=ImageTk.PhotoImage(img1)
    im2=ImageTk.PhotoImage(img2)

    labelPic1['image']=im1
    labelPic2['image']=im2

    labelPic1.image=im1
    labelPic2.image=im2

curP=0
height,width=448,448
suffix=('.jpg','.gif','.png')
pictures=[i for i in os.listdir("datasets-test/0") if i.endswith(suffix)]

root=tkinter.Tk()
root.title("船舶相似匹配")
root.geometry("960x700+280+20")#q前两个是窗口大小，后两个是窗口默认坐标

def shiplist(path):
    print(path)
    file = os.listdir(path)
    file.sort(key=lambda x: int(x))
    test_imgs = []
    for character in file:
        character_path = os.path.join(path, character)
        for image in os.listdir(character_path):
            test_imgs.append(os.path.join(character_path, image))
    return test_imgs

def openPath():
    path_ = tkinter.filedialog.askdirectory()
    path.set(path_)
    file = path_
    global shippathlist
    shippathlist = shiplist(file)

# shiplist("D:/ray/Siamese-pytorch-master/datasets-test")

#两张照片的位置
labelPic1=tkinter.Label(root,text='X',width=448,height=448)
labelPic1.place(x=25,y=25,width=448,height=448)

labelPic2=tkinter.Label(root,text='Y',width=448,height=448)
labelPic2.place(x=480,y=25,width=448,height=448)

#图标控件的位置
path = tkinter.StringVar()
#准确率值
value = tkinter.StringVar()
# path.set(os.path.abspath("."))

labelfile = tkinter.Label(root, text="路径:")
labelfile.place(x=10,y=500,width=30,height=35)
labelfileEntry = tkinter.Entry(root, textvariable=path)
labelfileEntry.place(x=40,y=500,width=240,height=35)

btnNext=tkinter.Button(root,text="选择路径",command=lambda:openPath())
btnNext.place(x=270,y=500,width=90,height=35)

btnPre=tkinter.Button(root,text='随机选择预配对',command=lambda:clickPre())
btnPre.place(x=380,y=500,width=120,height=35)

btnNext=tkinter.Button(root,text="随机选两张",command=lambda:clickRandom())
btnNext.place(x=510,y=500,width=120,height=35)

btnNext=tkinter.Button(root,text="匹配",command=lambda:predict())
btnNext.place(x=680,y=500,width=90,height=35)

l = tkinter.Label(
    text = "相似度:",
    font=('Arial', 12),     # 字体和字体大小
    width=60, height=35  # 标签长宽（以字符长度计算）
    )
l.place(x=780,y=500,width=50,height=35)

result_data_Text = tkinter.Entry(root,state='disabled',textvariable = value)
result_data_Text.place(x=850,y=500,width=90,height=35)

model = Siamese()

root.mainloop()

